const SCRIPT_LIST = document.querySelector('#SCRIPT_LIST');
const REFRESH = document.querySelector('#REFRESH');
const ADD = document.querySelector('#ADD');
const ALTER = document.querySelector('#ALTER');
const DELETE = document.querySelector('#DELETE');
const VALIDATE = document.querySelector('#VALIDATE');
const FIRE = document.querySelector('#FIRE');
const NOTE = document.querySelector('#NOTE');
const SCRIPT = document.querySelector('#SCRIPT');

let cur_id = 0;

const drawTable = rows => {
    SCRIPT_LIST.textContent = '';
    let tr = document.createElement("tr");
    tr.innerHTML = `
        <th>序号</th>
        <th>选择</th>
        <th>时间</th>
        <th>注记</th>
    `
    SCRIPT_LIST.appendChild(tr);

    for (let i = 0; i < rows.length; i++) {
        tr = document.createElement("tr");
        tr.innerHTML = `
        <td>${i + 1}</td>
        <td><input type="radio" name="ID" onchange="changeID(${rows[i].id})" /></td>
        <td>${formatDate((new Date(rows[i].c_time)))}</td>
        <td>${rows[i].note}</td>`;
        SCRIPT_LIST.appendChild(tr);
    }
};

const listTable = () => {
    cur_id = 0;
    getJson('/action/trigger', rows => {
        console.log('all Table, length:', rows.length);

        drawTable(rows);
    });
}

listTable();

REFRESH.addEventListener('click', () => {
    listTable();
});

const addOne = () => {
    let note = NOTE.value || '';
    let script = btoa(SCRIPT.value);
    putData(`/action/trigger`, { note, script }, () => {
        listTable();
        changeID();
    });
}

const altOne = id => {
    console.log('alter trigger', id);
    let note = NOTE.value || '';
    let script = btoa(SCRIPT.value);
    patchData(`/action/trigger/${id}`, { note, script }, () => {
        listTable();
    });
}

const delOne = id => {
    console.log('delete trigger', id)
    delAsk(`/action/trigger/${id}`, () => {
        listTable();
        changeID();
    })
}

const changeID = id => {
    if (!id) {
        NOTE.value = '';
        SCRIPT.value = '';
        return null;
    }

    cur_id = id;
    getJson(`/action/trigger/${id}`, row => {
        NOTE.value = row.note;
        try {
            SCRIPT.value = atob(row.script);
        } catch (err) {
            alert('代码解析错误');
        }
    });
}

validateScript = () => {
    let script = btoa(SCRIPT.value);
    postData('/action/trigger/vali', { script }, rtn => {
        console.log('validate:', rtn.flag);
        alert(rtn.flag ? '脚本校验通过' : '脚本校验失败');
    });
}

testScript = () => {
    let script = btoa(SCRIPT.value);
    postData('/action/trigger/test', { script }, rtn => {
        console.log('test:', rtn.flag);
        alert(rtn.flag ? '成功触发' : '未能触发');
    });
}
ADD.addEventListener('click', () => {
    addOne();
});
ALTER.addEventListener('click', () => {
    if (confirm('确认修改?')) altOne(cur_id);
});
DELETE.addEventListener('click', () => {
    if (confirm('确认删除?')) delOne(cur_id);
});
VALIDATE.addEventListener('click', () => {
    validateScript();
});
FIRE.addEventListener('click', () => {
    testScript();
});